package com.ezjie.ielts.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Paint.Cap;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;

import com.ezjie.ielts.R;
import com.ezjie.ielts.util.DensityUtil;

/**
 * 圆形进度条 单词首页
 * 
 * @author renyangyang
 * 
 */
public class WordHomeRoundProgressBar extends View {
	private static final int MSG_FIRST = 111;
	private static final float MAX_INCREASE = 0.1f;
	private static final float MIN_INCREASE = 0.05f;
	private static final int CYCLE_TIME = 40;// 循环时间50ms
	/**
	 * 画笔对象的引用
	 */
	private Paint paint;

	/**
	 * 圆环的颜色
	 */
	private int roundColor;

	/**
	 * 圆环进度的颜色
	 */
	private int roundProgressColor;

	/**
	 * 中间进度百分比的字符串的颜色
	 */
	private int textColor;

	/**
	 * 中间进度百分比的字符串的字体
	 */
	private float textSize;

	/**
	 * 圆环的宽度
	 */
	private float roundWidth;

	/**
	 * 最大进度
	 */
	private int max;

	/**
	 * 当前进度
	 */
	private int progress;
	private int targetProgress;
	private int firstNum;// 增长的数据
	/**
	 * 是否显示中间的进度
	 */
	private boolean textIsDisplayable;

	/**
	 * 进度的风格，实心或者空心
	 */
	private int style;
	/**
	 * 未同步的时间 只显示分钟部分
	 */
	private int unSynTime;
	private int secondProgressColor;// 第二进度有值的时候颜色
	private Context mContext;

	public static final int STROKE = 0;
	public static final int FILL = 1;

	public WordHomeRoundProgressBar(Context context) {
		this(context, null);
		this.mContext = context;
	}

	public WordHomeRoundProgressBar(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
		this.mContext = context;
	}

	public WordHomeRoundProgressBar(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.mContext = context;

		paint = new Paint();

		TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundProgressBar);

		// 获取自定义属性和默认值
		roundColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.RED);
		roundProgressColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.GREEN);
		textColor = mTypedArray.getColor(R.styleable.RoundProgressBar_textColor, Color.GREEN);
		textSize = mTypedArray.getDimension(R.styleable.RoundProgressBar_textSize, 15);
		roundWidth = mTypedArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 5);
		max = mTypedArray.getInteger(R.styleable.RoundProgressBar_max, 100);
		textIsDisplayable = mTypedArray.getBoolean(R.styleable.RoundProgressBar_textIsDisplayable, false);
		style = mTypedArray.getInt(R.styleable.RoundProgressBar_style, 0);
		mTypedArray.recycle();

		secondProgressColor = Color.rgb(235, 115, 88);// eb7358
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		/**
		 * 画最外层的大圆环
		 */
		int roundWidth = DensityUtil.dip2px(getContext(), 7);
		int centre = getWidth() / 2; // 获取圆心的x坐标
		int radius = centre - roundWidth / 2; // 圆环的半径
		RectF oval = new RectF(centre - radius, centre - radius, centre + radius, centre + radius); // 用于定义的圆弧的形状和大小的界限

		paint.setColor(Color.rgb(228, 228, 228)); // 设置圆环的颜色
		paint.setStyle(Paint.Style.STROKE); // 设置空心
		paint.setStrokeWidth(roundWidth); // 设置圆环的宽度
		paint.setAntiAlias(true); // 消除锯齿
		paint.setStrokeCap(Cap.ROUND);// 设置笔刷的形状
		// canvas.drawCircle(centre, centre, radius, paint); //画出圆环
		paint.setShader(null);
		canvas.drawArc(oval, -210, 240, false, paint);

		/**
		 * 画进度百分比 drawText1 878999 在中心基础上 减10dp
		 */
		paint.setStrokeWidth(0);
		/*
		 * if (unSynTime > 0) { paint.setColor(secondProgressColor); }else{
		 * paint.setColor(Color.rgb(135, 137, 153)); }
		 */
		paint.setColor(Color.rgb(255, 178, 0));
		float textSize1 = getResources().getDimension(R.dimen.word_size);
		paint.setTextSize(textSize1);
		paint.setTypeface(Typeface.DEFAULT_BOLD); // 设置字体
		paint.setShader(null);
		// //中间的进度百分比，先转换成float在进行除法运算，不然都为0
		// int percent = (int) (((float) progress / (float) max) * 100);
		// //测量字体宽度，我们需要根据字体的宽度设置在圆环中间
		// float textWidth1 = paint.measureText(unSynTime+"" );

		// canvas.drawText(unSynTime+"", centre - textWidth1 / 2, centre
		// + textSize1 / 2-DensityUtil.dip2px(getContext(), 10), paint); //
		// 画出进度百分比
		float textWidth1 = paint.measureText(progress + "");

		canvas.drawText(progress + "", centre - textWidth1 / 2,
				centre + textSize1 / 2 - DensityUtil.dip2px(getContext(), 10), paint); // 画出进度百分比
		/**
		 * 画min drawText2 8b8b8b 在drawText1基础上加 10dp
		 */
		/*
		 * if (unSynTime > 0) { paint.setColor(secondProgressColor); }else{
		 * paint.setColor(Color.rgb(139, 139, 139)); }
		 */
		paint.setColor(Color.rgb(208, 208, 208));
		float textSize2 = getResources().getDimension(R.dimen.little_size);
		paint.setTextSize(textSize2);
		paint.setTypeface(Typeface.DEFAULT);
		paint.setShader(null);
		String getWord = mContext.getResources().getString(R.string.word_practice_get_word);
		float textWidth2 = paint.measureText(getWord);
		canvas.drawText(getWord, centre - textWidth2 / 2,
				centre + textSize1 / 2 + DensityUtil.dip2px(getContext(), 10), paint); // 画出MIN
		/**
		 * 画待同步时长 drawText3 8b8b8b 在drawText2基础上加 15dp
		 */
		/*
		 * paint.setColor(Color.rgb(139, 139, 139)); float textWidth3 =
		 * paint.measureText("待同步时长"); canvas.drawText("待同步时长", centre -
		 * textWidth3 / 2, centre + textSize2 / 2 + textSize2 / 2 + textSize1 /
		 * 2 +DensityUtil.dip2px(getContext(), 15), paint);
		 */// 画出待同步时长

		paint.setColor(Color.rgb(251, 113, 11)); // 设置进度的颜色
		paint.setStyle(Paint.Style.STROKE);
		paint.setStrokeWidth(roundWidth); // 设置圆环的宽度
		paint.setShader(null);
		canvas.drawArc(oval, -210, 240 * 50 / 5073, false, paint); // 显示一个默认的圆点

		/**
		 * 画圆弧 ，画圆环的进度
		 */

		paint.setStrokeWidth(roundWidth); // 设置圆环的宽度

		/**
		 * 画第一进度
		 */
		paint.setColor(Color.rgb(251, 113, 11)); // 设置进度的颜色
		LinearGradient shader = new LinearGradient(centre - radius, centre - radius, centre + radius, centre + radius,
				Color.rgb(251, 113, 11), Color.rgb(255, 178, 0), Shader.TileMode.CLAMP);
		paint.setShader(shader);
		// paint.setStrokeCap(Cap.ROUND);// 设置笔刷的形状
		switch (style) {
		case STROKE: {
			paint.setStyle(Paint.Style.STROKE);
			if (progress != 0) {
				canvas.drawArc(oval, -210, 240 * progress / max, false, paint); // 根据进度画圆弧
			}
			break;
		}
		case FILL: {
			paint.setStyle(Paint.Style.FILL_AND_STROKE);
			if (progress != 0)
				canvas.drawArc(oval, -210, 240 * progress / max, true, paint); // 根据进度画圆弧
			break;
		}
		}

	}

	/*
	 * @Override protected void onMeasure(int widthMeasureSpec, int
	 * heightMeasureSpec) { // 计算组件的宽和高 DisplayMetrics dm = new
	 * DisplayMetrics(); dm =
	 * mContext.getApplicationContext().getResources().getDisplayMetrics(); int
	 * height = dm.heightPixels; int menuBar = PreferencesUtil.getInt(mContext,
	 * WordHomeDataUtil.HEIGHT_MENU_BAR, 0); int titleBar =
	 * PreferencesUtil.getInt(mContext, WordHomeDataUtil.HEIGHT_TITLE_BAR, 0);
	 * int viewWidth = (height-DensityUtil.dip2px(mContext,
	 * 69)-menuBar-titleBar)/5*2+1-DensityUtil.dip2px(mContext, 25+60+25);
	 * setMeasuredDimension(viewWidth, viewWidth);
	 * 
	 * DisplayMetrics dm = new DisplayMetrics(); dm =
	 * mContext.getApplicationContext().getResources().getDisplayMetrics(); int
	 * height = dm.heightPixels; int viewWidth =
	 * (height-DensityUtil.dip2px(mContext,
	 * 69))/5*2+1-DensityUtil.dip2px(mContext, 25+60+25);
	 * setMeasuredDimension(viewWidth, viewWidth); }
	 */

	public synchronized int getMax() {
		return max;
	}

	/**
	 * 设置进度的最大值
	 * 
	 * @param max
	 */
	public synchronized void setMax(int max) {
		if (max < 0) {
			throw new IllegalArgumentException("max not less than 0");
		}
		this.max = max;
	}

	/**
	 * 设置 未同步的 时间
	 * 
	 * @param unSynTime
	 */
	public synchronized void setSecondProgress(int unSynTime) {
		this.unSynTime = unSynTime;
		postInvalidate();
	}

	public synchronized int getSecondProgress() {
		return unSynTime;
	}

	/**
	 * 获取进度.需要同步
	 * 
	 * @return
	 */
	public synchronized int getProgress() {
		return progress;
	}

	/**
	 * 设置进度，此为线程安全控件，由于考虑多线的问题，需要同步 刷新界面调用postInvalidate()能在非UI线程刷新
	 * 
	 * @param progress
	 */
	public synchronized void setProgress(int progress) {
		if (progress < 0) {
			throw new IllegalArgumentException("progress not less than 0");
		}
		if (progress > max) {
			progress = max;
		}
		if (progress <= max) {
			this.progress = progress;
			postInvalidate();
		}

	}

	public synchronized void setAnimationProgress(int progress) {
		if (progress < 0) {
			throw new IllegalArgumentException("progress not less than 0");
		}
		if (progress > max) {
			progress = max;
		}
		if (progress <= max) {
			this.targetProgress = progress;
			this.progress = 0;
			firstNum = (int) ((targetProgress) * MAX_INCREASE);
			postInvalidate();

			Message msg = handler.obtainMessage();
			msg.what = MSG_FIRST;
			handler.sendMessageDelayed(msg, 0);
		}

	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case MSG_FIRST:
				progress += firstNum;
				if (progress >= targetProgress) {
					progress = targetProgress;
					postInvalidate();
					handler.removeMessages(MSG_FIRST);
					break;
				}
				postInvalidate();

				firstNum--;
				int minNum = (int) ((targetProgress) * MIN_INCREASE);
				if (minNum == 0) {
					minNum = 1;
				}
				if (firstNum <= minNum) {
					firstNum = minNum;
				}
				// 循环
				Message msg1 = handler.obtainMessage();
				msg1.what = MSG_FIRST;
				handler.sendMessageDelayed(msg1, CYCLE_TIME);
				break;

			default:
				break;
			}
		}
	};

	public int getCricleColor() {
		return roundColor;
	}

	public void setCricleColor(int cricleColor) {
		this.roundColor = cricleColor;
	}

	public int getCricleProgressColor() {
		return roundProgressColor;
	}

	public void setCricleProgressColor(int cricleProgressColor) {
		this.roundProgressColor = cricleProgressColor;
	}

	public int getTextColor() {
		return textColor;
	}

	public void setTextColor(int textColor) {
		this.textColor = textColor;
	}

	public float getTextSize() {
		return textSize;
	}

	public void setTextSize(float textSize) {
		this.textSize = textSize;
	}

	public float getRoundWidth() {
		return roundWidth;
	}

	public void setRoundWidth(float roundWidth) {
		this.roundWidth = roundWidth;
	}

}
